package com.ttxs.uaa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttxs.uaa.entity.SysRole;
import com.ttxs.uaa.exception.DuplicateException;
import com.ttxs.uaa.mapper.SysRoleMapper;
import com.ttxs.uaa.service.ISysRoleMenuService;
import com.ttxs.uaa.service.ISysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author 天天向上
 * @since 2021-07-26
 */
@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    private final ISysRoleMenuService sysRoleMenuService;

    @Override
    public boolean save(SysRole entity) {
        long count = this.count(new QueryWrapper<SysRole>().eq("role_name", entity.getRoleName())
            .eq("platform_id", entity.getPlatformId()));
        if(count > 0) {
            throw new DuplicateException("角色名称已经存在");
        }
        return super.save(entity);
    }

    @Override
    public boolean removeByIds(Collection<?> idList) {
        // 删除角色菜单关联表数据
        sysRoleMenuService.removeByRoleIds(idList);

        return super.removeByIds(idList);
    }
}
